const keyName = 's_'
/**
 * 存储localStorage
 */
const setStore = (name,content,type) => {
  name = keyName + name;
  if(typeof content === 'object'){
    content = JSON.stringify(content);
  }
  if (type) window.sessionStorage.setItem(name, content);
  else window.localStorage.setItem(name, content);
}
/**
 * 获取localStorage
 */

const getStore = (name,type) => {
  name = keyName + name;
  let content;
  if (type) content = window.sessionStorage.getItem(name);
  else content = window.localStorage.getItem(name);
  if (!isNaN(content)) { //纯数字
    return content;
  }else {//不全是数字
    try {
      let obj = JSON.parse(content);
      return obj;
    }catch (e) {
      return content
    }
  }
}
/**
 * 删除Storage
 */
const removeStore = (name,type) => {
  name = keyName + name
  if (type) window.sessionStorage.removeItem(name);
  window.localStorage.removeItem(name);


}

/**
 * 获取全部Storage
 */
const getAllStore = (type) => {
  let list = [];
  if (type) {
    for (let i = 0; i <= window.sessionStorage.length; i++) {
      list.push({
        name: window.sessionStorage.key(i),
        content: getStore(window.sessionStorage.key(i),type)
      })
    }
  } else {
    for (let i = 0; i <= window.localStorage.length; i++) {
      list.push({
        name: window.localStorage.key(i),
        content: getStore( window.localStorage.key(i),type)
      })

    }
  }
  return list;

}

/**
 * 清空全部Storage
 */
const clearStore = (type) => {
  if (type) window.sessionStorage.clear();
  window.localStorage.clear()

}
/**
 * 存储localStorage
 */
export const setLocalStore = (name,content) => {
  setStore(name,content);
}
/**
 * 获取localStorage
 */

export const getLocalStore = (name) => {
  return getStore(name)
}
/**
 * 删除localStorage
 */
export const removeLocalStore = (name) => {
  removeStore(name)
}

/**
 * 获取全部localStorage
 */
export const getAllLocalStore = () => {
  return getAllStore();
}

/**
 * 清空全部localStorage
 */
export const clearLocalStore = () => {
  clearStore();
}
/**
 * 存储sessionStorage
 */
export const setSessionStore = (name,content) => {
  setStore(name,content,true);
}
/**
 * 获取localStorage
 */

export const getSessionStore = (name) => {
  return getStore(name,true)
}
/**
 * 删除localStorage
 */
export const removeSessionStore = (name) => {
  removeStore(name,true)
}

/**
 * 获取全部localStorage
 */
export const getAllSessionStore = () => {
  return getAllStore(true);
}

/**
 * 清空全部localStorage
 */
export const clearSessionStore = () => {
  clearStore(true);
}


